



<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#FFF">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon.png">

<link rel="icon" type="image/ico" sizes="32x32" href="/blog/images/favicon.ico">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">


<link rel="alternate" type="application/rss+xml" title="Endless" href="http://fairy77.gitee.io/rss.xml" />
<link rel="alternate" type="application/atom+xml" title="Endless" href="http://fairy77.gitee.io/atom.xml" />
<link rel="alternate" type="application/json" title="Endless" href="http://fairy77.gitee.io/feed.json" />

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/blog/css/app.css?v=0.2.5">

  
  <meta name="keywords" content="Nginx" />


<link rel="canonical" href="http://fairy77.gitee.io/2020/03/03/Nginx/">



  <title>
Nginx 学习笔记 |
夏季大三角 = Endless</title>
<meta name="generator" content="Hexo 5.4.0"></head>
<body itemscope itemtype="http://schema.org/WebPage">
  <div id="loading">
    <div class="cat">
      <div class="body"></div>
      <div class="head">
        <div class="face"></div>
      </div>
      <div class="foot">
        <div class="tummy-end"></div>
        <div class="bottom"></div>
        <div class="legs left"></div>
        <div class="legs right"></div>
      </div>
      <div class="paw">
        <div class="hands left"></div>
        <div class="hands right"></div>
      </div>
    </div>
  </div>
  <div id="container">
    <header id="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="inner">
        <div id="brand">
          <div class="pjax">
          
  <h1 itemprop="name headline">Nginx 学习笔记
  </h1>
  
<div class="meta">
  <span class="item" title="创建时间：2020-03-03 14:34:39">
    <span class="icon">
      <i class="ic i-calendar"></i>
    </span>
    <span class="text">发表于</span>
    <time itemprop="dateCreated datePublished" datetime="2020-03-03T14:34:39+08:00">2020-03-03</time>
  </span>
  <span class="item" title="本文字数">
    <span class="icon">
      <i class="ic i-pen"></i>
    </span>
    <span class="text">本文字数</span>
    <span>6.2k</span>
    <span class="text">字</span>
  </span>
  <span class="item" title="阅读时长">
    <span class="icon">
      <i class="ic i-clock"></i>
    </span>
    <span class="text">阅读时长</span>
    <span>6 分钟</span>
  </span>
</div>


          </div>
        </div>
        <nav id="nav">
  <div class="inner">
    <div class="toggle">
      <div class="lines" aria-label="切换导航栏">
        <span class="line"></span>
        <span class="line"></span>
        <span class="line"></span>
      </div>
    </div>
    <ul class="menu">
      <li class="item title"><a href="/blog/" rel="start">夏季大三角</a></li>
    </ul>
    <ul class="right">
      <li class="item theme">
        <i class="ic i-sun"></i>
      </li>
      <li class="item search">
        <i class="ic i-search"></i>
      </li>
    </ul>
  </div>
</nav>

      </div>
      <div id="imgs" class="pjax">
        <ul>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1giclx6phq6j20zk0m8e36.jpg"></li>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1gipeu1usa7j20zk0m8b29.jpg"></li>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1gipey84bjtj20zk0m8hdt.jpg"></li>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1gipexbei4hj20zk0m8npd.jpg"></li>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1gicli3sbvtj20zk0m8x6p.jpg"></li>
          <li class="item" data-background-image="https://tva1.sinaimg.cn/large/6833939bly1giclj61ylzj20zk0m8b29.jpg"></li>
        </ul>
      </div>
    </header>
    <div id="waves">
      <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
        <defs>
          <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" />
        </defs>
        <g class="parallax">
          <use xlink:href="#gentle-wave" x="48" y="0" />
          <use xlink:href="#gentle-wave" x="48" y="3" />
          <use xlink:href="#gentle-wave" x="48" y="5" />
          <use xlink:href="#gentle-wave" x="48" y="7" />
        </g>
      </svg>
    </div>
    <main>
      <div class="inner">
        <div id="main" class="pjax">
          
  <div class="article wrap">
    
<div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
<i class="ic i-home"></i>
<span><a href="/blog/">首页</a></span>
</div>

    <article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN">
  <link itemprop="mainEntityOfPage" href="http://fairy77.gitee.io/2020/03/03/Nginx/">

  <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="image" content="/blog/images/avatar.jpg">
    <meta itemprop="name" content="Rain Bear">
    <meta itemprop="description" content=", ">
  </span>

  <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Endless">
  </span>

  <div class="body md" itemprop="articleBody">
    

    <p><span class="exturl" data-url="aHR0cHM6Ly93d3cuYmlsaWJpbGkuY29tL3ZpZGVvL2F2NjgxMzY3MzQv">参考视频</span></p>
<h2 id="1-nginx基本概念"><a class="markdownIt-Anchor" href="#1-nginx基本概念">#</a> 1. Nginx 基本概念</h2>
<h3 id="11-什么是nginx"><a class="markdownIt-Anchor" href="#11-什么是nginx">#</a> 1.1 什么是 nginx</h3>
<p>Nginx（“engine x”）是一个高性能的 HTTP 和反向代理服务器，特点是占有内存少，并发能力强，</p>
<span id="more"></span>
<p>事实上 nginx 的并发能力确实在同类型的网页服务器中表现较好。<br>
Nginx 专为性能优化而开发，性能是其最重要的考量，实现上非常<br>
注重效率，能经受高负载的考验，有报告表明能支持高达 50,000 个并发连接数。<br>
<span class="exturl" data-url="aHR0cHM6Ly9sbm1wLm9yZy9uZ2lueC5odG1s">https://lnmp.org/nginx.html</span></p>
<h3 id="12-反向代理"><a class="markdownIt-Anchor" href="#12-反向代理">#</a> 1.2 反向代理</h3>
<h4 id="121-正向代理"><a class="markdownIt-Anchor" href="#121-正向代理">#</a> 1.2.1 正向代理</h4>
<ul>
<li>在客户端（浏览器）配置代理服务器，通过代理服务器进行互联网访问。<br>
<img data-src="Nginx/%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86.jpg" alt=""></li>
</ul>
<h4 id="122-反向代理"><a class="markdownIt-Anchor" href="#122-反向代理">#</a> 1.2.2 反向代理</h4>
<ul>
<li>我们只需要将请求发送到反向代理服务器，由反向代理服务器去选择目标服务器获取数据后，<br>
在返回给客户端，此时反向代理服务器和目标服务器对外就是一个服务器，暴露的是代理服务器地址，隐藏了真实服务器 IP 地址。<br>
<img data-src="Nginx/%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86.jpg" alt=""></li>
</ul>
<h3 id="13-负载均衡"><a class="markdownIt-Anchor" href="#13-负载均衡">#</a> 1.3 负载均衡</h3>
<ul>
<li>单个服务器解决不了，我们增加服务器数量，然后将请求分发到各个服务器上，将原先请求集中到单个服务器上的情况改成将请求分发<br>
到多个服务器上，将负载分发到不同的服务器，也就是我们所说的负载均衡。<br>
<img data-src="Nginx/%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1.jpg" alt=""></li>
</ul>
<h3 id="14-动静分离"><a class="markdownIt-Anchor" href="#14-动静分离">#</a> 1.4 动静分离</h3>
<ul>
<li>为了加快网站的解析速度，可以把动态页面和静态页面有不同的服务器来解析，加快解析速度。降低原来单个服务器的压力。<br>
<img data-src="Nginx/%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB.jpg" alt=""></li>
</ul>
<h2 id="2-nginx安装"><a class="markdownIt-Anchor" href="#2-nginx安装">#</a> 2. Nginx 安装</h2>
<h3 id="21-在linux系统中安装nginx"><a class="markdownIt-Anchor" href="#21-在linux系统中安装nginx">#</a> 2.1 在 Linux 系统中安装 nginx</h3>
<ul>
<li>2.1.1 官网下载.tar.gz 包</li>
<li>2.1.2 安装前需要安装 4 个依赖包
<ul>
<li><code>yum install gcc-c++</code>  编译环境</li>
<li><code>yum install pcre-devel</code>  Nginx 的 http 模块需要使用 pcre 来解析正则表达式</li>
<li><code>yum install zlib zlib-devel</code>  依赖的解压包</li>
<li><code>yum install openssl openssl-devel</code></li>
<li><code>yum -y install gcc-c++ zlib zlib-devel pcre-devel openssl openssl-devel</code>  // 一键安装上面四个依赖</li>
</ul>
</li>
<li>2.1.3 安装 nginx
<ul>
<li>解压缩 nginx.xx.tar.gz 包</li>
<li>进入解压目录执行./configure</li>
<li>make &amp;&amp; make install</li>
<li>开放端口（CentOS7）
<ul>
<li><code>firewall-cmd --list-all</code>  查看防火墙情况</li>
<li><code>firewall-cmd --zone=public(作用域) --add-port=80/tcp(端口和访问类型) --permanent(永久生效)</code></li>
<li><code>firewall-cmd --reload</code>  重启防火墙</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="22-nginx常用命令"><a class="markdownIt-Anchor" href="#22-nginx常用命令">#</a> 2.2 nginx 常用命令</h3>
<ul>
<li><code>./nginx -v</code>  查看 nginx 版本号</li>
<li><code>./nginx</code>  启动 nginx<br>
<img data-src="Nginx/nginx_start.jpg" alt=""></li>
<li><code>./nginx -s stop</code>  关闭 nginx<br>
<img data-src="Nginx/nginx_stop.jpg" alt=""></li>
<li><code>./nginx -s reload</code>  重新加载 nginx（一般用于配置文件修改后生效）</li>
</ul>
<h3 id="23-nginx配置文件"><a class="markdownIt-Anchor" href="#23-nginx配置文件">#</a> 2.3 nginx 配置文件</h3>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line">#user  nobody;</span><br><span class="line">worker_processes  1;</span><br><span class="line"></span><br><span class="line">#error_log  logs/error.log;</span><br><span class="line">#error_log  logs/error.log  notice;</span><br><span class="line">#error_log  logs/error.log  info;</span><br><span class="line"></span><br><span class="line">#pid        logs/nginx.pid;</span><br><span class="line"></span><br><span class="line">events &#123;</span><br><span class="line">    worker_connections  1024;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">http &#123;</span><br><span class="line">    include       mime.types;</span><br><span class="line">    default_type  application/octet-stream;</span><br><span class="line"></span><br><span class="line">    #log_format  main  &#x27;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#x27;</span><br><span class="line">    #                  &#x27;$status $body_bytes_sent &quot;$http_referer&quot; &#x27;</span><br><span class="line">    #                  &#x27;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot;&#x27;;</span><br><span class="line"></span><br><span class="line">    #access_log  logs/access.log  main;</span><br><span class="line"></span><br><span class="line">    sendfile        on;</span><br><span class="line">    #tcp_nopush     on;</span><br><span class="line"></span><br><span class="line">    #keepalive_timeout  0;</span><br><span class="line">    keepalive_timeout  65;</span><br><span class="line"></span><br><span class="line">    #gzip  on;</span><br><span class="line"></span><br><span class="line">    server &#123;</span><br><span class="line">        listen       80;</span><br><span class="line">        server_name  localhost;</span><br><span class="line"></span><br><span class="line">        #charset koi8-r;</span><br><span class="line"></span><br><span class="line">        #access_log  logs/host.access.log  main;</span><br><span class="line"></span><br><span class="line">        location / &#123;</span><br><span class="line">            root   html;</span><br><span class="line">            index  index.html index.htm;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        #error_page  404              /404.html;</span><br><span class="line"></span><br><span class="line">        # redirect server error pages to the static page /50x.html</span><br><span class="line">        #</span><br><span class="line">        error_page   500 502 503 504  /50x.html;</span><br><span class="line">        location = /50x.html &#123;</span><br><span class="line">            root   html;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        # proxy the PHP scripts to Apache listening on 127.0.0.1:80</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    proxy_pass   http://127.0.0.1;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000</span><br><span class="line">        #</span><br><span class="line">        #location ~ \.php$ &#123;</span><br><span class="line">        #    root           html;</span><br><span class="line">        #    fastcgi_pass   127.0.0.1:9000;</span><br><span class="line">        #    fastcgi_index  index.php;</span><br><span class="line">        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;</span><br><span class="line">        #    include        fastcgi_params;</span><br><span class="line">        #&#125;</span><br><span class="line"></span><br><span class="line">        # deny access to .htaccess files, if Apache&#x27;s document root</span><br><span class="line">        # concurs with nginx&#x27;s one</span><br><span class="line">        #</span><br><span class="line">        #location ~ /\.ht &#123;</span><br><span class="line">        #    deny  all;</span><br><span class="line">        #&#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    # another virtual host using mix of IP-, name-, and port-based configuration</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       8000;</span><br><span class="line">    #    listen       somename:8080;</span><br><span class="line">    #    server_name  somename  alias  another.alias;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    # HTTPS server</span><br><span class="line">    #</span><br><span class="line">    #server &#123;</span><br><span class="line">    #    listen       443 ssl;</span><br><span class="line">    #    server_name  localhost;</span><br><span class="line"></span><br><span class="line">    #    ssl_certificate      cert.pem;</span><br><span class="line">    #    ssl_certificate_key  cert.key;</span><br><span class="line"></span><br><span class="line">    #    ssl_session_cache    shared:SSL:1m;</span><br><span class="line">    #    ssl_session_timeout  5m;</span><br><span class="line"></span><br><span class="line">    #    ssl_ciphers  HIGH:!aNULL:!MD5;</span><br><span class="line">    #    ssl_prefer_server_ciphers  on;</span><br><span class="line"></span><br><span class="line">    #    location / &#123;</span><br><span class="line">    #        root   html;</span><br><span class="line">    #        index  index.html index.htm;</span><br><span class="line">    #    &#125;</span><br><span class="line">    #&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="231-配置文件位置根据安装类型不同位置可能不一样也可以自行配置地址"><a class="markdownIt-Anchor" href="#231-配置文件位置根据安装类型不同位置可能不一样也可以自行配置地址">#</a> 2.3.1 配置文件位置（根据安装类型不同，位置可能不一样，也可以自行配置地址）</h4>
<p>/usr/local/nginx/conf/nginx.conf<br>
<img data-src="Nginx/%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE.jpg" alt=""></p>
<h4 id="232-nginx配置文件组成"><a class="markdownIt-Anchor" href="#232-nginx配置文件组成">#</a> 2.3.2 nginx 配置文件组成</h4>
<ul>
<li>nginx 配置文件有三部分组成
<ul>
<li>1）全局块
<ul>
<li>配置文件开始到 events 块之间的内容，主要会设置一些影响 nginx 服务器整体运行的配置指令。<br>
比如  <code>worker_processes  1;</code> ，worker_processes 值越大，可以支持的并发处理量也越多。</li>
</ul>
</li>
<li>2）events 块
<ul>
<li>events 块涉及的指令主要影响 nginx 服务器与用户的网络连接。<br>
比如 <code>worker_connections  1024;</code> ，支持的最大连接数</li>
</ul>
</li>
<li>3）http 块
<ul>
<li>Nginx 服务器配置中最频繁的部分，也包括 http 全局块、server 块</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="3-nginx配置实例1-反向代理"><a class="markdownIt-Anchor" href="#3-nginx配置实例1-反向代理">#</a> 3. Nginx 配置实例 1 - 反向代理</h2>
<h3 id="31-实现效果"><a class="markdownIt-Anchor" href="#31-实现效果">#</a> 3.1 实现效果</h3>
<ul>
<li>打开浏览器，<span class="exturl" data-url="aHR0cDovL3huLS13d3ctMHYxZTQydTU2YXdiYjZsYzQ0NDhieWZubWsxZ2w3cS4xMjMuY29t">在浏览器地址栏输入地址 www.123.com</span>, 跳转到 Linux 系统 tomcat 主页面中。</li>
</ul>
<h3 id="32-准备工作"><a class="markdownIt-Anchor" href="#32-准备工作">#</a> 3.2 准备工作</h3>
<ul>
<li>1）在 Linux 系统中安装 tomcat，使用默认端口号 8080</li>
<li>
<ol start="2">
<li><span class="exturl" data-url="aHR0cDovL3huLS1ob3N0c25naW54d3d3LXR0M3VpODBkaXlsMWhhODE1cWc2ai4xMjMuY29t">hosts 文件 nginx 地址指向 www.123.com</span></li>
</ol>
</li>
</ul>
<h3 id="33-访问过程分析"><a class="markdownIt-Anchor" href="#33-访问过程分析">#</a> 3.3 访问过程分析</h3>
<p><img data-src="Nginx/conf_%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E5%88%86%E6%9E%90.jpg" alt=""></p>
<h3 id="34-nginx配置"><a class="markdownIt-Anchor" href="#34-nginx配置">#</a> 3.4 nginx 配置</h3>
<p><img data-src="Nginx/conf_%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86.jpg" alt=""></p>
<ul>
<li>补：
<ul>
<li><img data-src="Nginx/conf_%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%862.jpg" alt=""></li>
<li>location 指令说明  <code>location [ = | ~ | ~* | ^~] uri &#123;&#125;</code>
<ul>
<li>1） = : 用于不含正则表达式的 uri 前，要求请求字符串和 uri 严格匹配。</li>
<li>2） ~ ：用于表示 uri 正则表达式，区分大小写。</li>
<li>3） ~* ：用于表示 uri 正则表达式，不区分大小写。</li>
<li>4） ^~ : 用于不含正则表达式的 uri 前，要求 nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后，立即使用此 location<br>
 处理请求，而不再使用 location 块中的正则 uri 和请求字符串匹配</li>
<li>注意 ： 如果 uri 包含正则表达式，则必须要有<sub>或者</sub> * 标识。</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="4-nginx配置实例2-负载均衡"><a class="markdownIt-Anchor" href="#4-nginx配置实例2-负载均衡">#</a> 4. Nginx 配置实例 2 - 负载均衡</h2>
<h3 id="41-实现效果"><a class="markdownIt-Anchor" href="#41-实现效果">#</a> 4.1 实现效果</h3>
<ul>
<li>1）浏览器地址栏输入地址 <span class="exturl" data-url="aHR0cDovLzE3NS4yMC4yNC44NS9lZHUvYS5odG1sJUVGJUJDJThDJUU4JUI0JTlGJUU4JUJEJUJEJUU1JTlEJTg3JUU4JUExJUExJUU2JTk1JTg4JUU2JTlFJTlDJUVGJUJDJThDJUU1JUI5JUIzJUU1JTlEJTg3ODA4MCVFNSU5MiU4QzgwODElRTclQUIlQUYlRTUlOEYlQTMlRTQlQjglQUQ=">http://175.20.24.85/edu/a.html，负载均衡效果，平均 8080 和 8081 端口中</span></li>
</ul>
<h3 id="42-准备工作"><a class="markdownIt-Anchor" href="#42-准备工作">#</a> 4.2 准备工作</h3>
<ul>
<li>1）两台 tomcat 服务器，一台 8080，一台 8081</li>
<li>2）在两台 tomcat 里面 webapps 目录中，创建名称是 edu 文件夹，在 edu 文件夹中创建页面 a.html，用于测试</li>
</ul>
<h3 id="43-在nginx的配置文件中进行负载均衡的配置"><a class="markdownIt-Anchor" href="#43-在nginx的配置文件中进行负载均衡的配置">#</a> 4.3 在 nginx 的配置文件中进行负载均衡的配置</h3>
<p><img data-src="Nginx/conf_%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1.jpg" alt=""></p>
<h3 id="44-nginx分配服务器策略"><a class="markdownIt-Anchor" href="#44-nginx分配服务器策略">#</a> 4.4 nginx 分配服务器策略</h3>
<ul>
<li>1）轮询（默认）：每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器 down 掉，能自动剔除</li>
<li>2）weight：代表权重默认为 1, 权重越高被分配的客户端越多</li>
<li>3）ip_hash：每个请求按访问 ip 的 hash 结果分配，这样每个访客固定访问一个后端服务器</li>
<li>4）fair (第三方)：按后端服务器的响应时间来分配请求，响应时间短的优先分配。</li>
</ul>
<h2 id="5-nginx配置实例3-动静分离"><a class="markdownIt-Anchor" href="#5-nginx配置实例3-动静分离">#</a> 5. Nginx 配置实例 3 - 动静分离</h2>
<h3 id="51-什么是动静分离"><a class="markdownIt-Anchor" href="#51-什么是动静分离">#</a> 5.1 什么是动静分离</h3>
<p><img data-src="Nginx/conf_%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB%E5%90%AB%E4%B9%89.jpg" alt=""></p>
<ul>
<li>通过 location 指定不同的后缀名实现不同的请求转发。通过 expires 参数设置，可以使浏<br>
览器缓存过期时间，减少与服务器之前的请求和流量。具体 Expires 定义：是给一个资源<br>
设定一个过期时间，也就是说无需去服务端验证，直接通过浏览器自身确认是否过期即可，<br>
所以不会产生额外的流量。此种方法非常适合不经常变动的资源。（如果经常更新的文件，<br>
不建议使用 Expires 来缓存），我这里设置 3d，表示在这 3 天之内访问这个 URL，发送一<br>
个请求，比对服务器该文件最后更新时间没有变化，则不会从服务器抓取，返回状态码 304，<br>
如果有修改，则直接从服务器重新下载，返回状态码 200。</li>
</ul>
<h3 id="52-准备工作"><a class="markdownIt-Anchor" href="#52-准备工作">#</a> 5.2 准备工作</h3>
<ul>
<li>在 liunx 系统中准备静态资源，用于进行访问。</li>
</ul>
<h3 id="53-具体配置"><a class="markdownIt-Anchor" href="#53-具体配置">#</a> 5.3 具体配置</h3>
<p><img data-src="Nginx/conf_%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB_%E9%85%8D%E7%BD%AE.jpg" alt=""></p>
<h2 id="6-nginx配置高可用集群"><a class="markdownIt-Anchor" href="#6-nginx配置高可用集群">#</a> 6. Nginx 配置高可用集群</h2>
<h3 id="61-什么是nginx高可用"><a class="markdownIt-Anchor" href="#61-什么是nginx高可用">#</a> 6.1 什么是 nginx 高可用</h3>
<p><img data-src="Nginx/%E9%AB%98%E5%8F%AF%E7%94%A8.jpg" alt=""></p>
<h3 id="62-准备工作"><a class="markdownIt-Anchor" href="#62-准备工作">#</a> 6.2 准备工作</h3>
<ul>
<li>1）两台 nginx 服务器</li>
<li>2）keepalived</li>
</ul>
<h3 id="63-安装keepalived"><a class="markdownIt-Anchor" href="#63-安装keepalived">#</a> 6.3 安装 keepalived</h3>
<ul>
<li>
<ol>
<li><code>yum install keepalived -y</code></li>
</ol>
</li>
<li>2）安装完后，/etc/keepalived/keepalived.conf 配置文件</li>
</ul>
<h3 id="64-配置"><a class="markdownIt-Anchor" href="#64-配置">#</a> 6.4 配置</h3>
<ul>
<li>1）修改 keepalived.conf 配置文件</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123;</span><br><span class="line">        notification_email &#123;</span><br><span class="line">        acassen@firewall.loc</span><br><span class="line">        failover@firewall.loc</span><br><span class="line">        sysadmin@firewall.loc</span><br><span class="line">    &#125;</span><br><span class="line">    notification_email_from Alexandre.Cassen@firewall.loc</span><br><span class="line">    smtp_server 192.168.17.129</span><br><span class="line">    smtp_connect_timeout 30</span><br><span class="line">    router_id LVS_DEVEL</span><br><span class="line">&#125;</span><br><span class="line">vrrp_script chk_http_port &#123;</span><br><span class="line">    script &quot;/usr/local/src/nginx_check.sh&quot;</span><br><span class="line">    interval 2 #检测脚本执行的间隔）</span><br><span class="line">    weight 2</span><br><span class="line">&#125;</span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state BACKUP #备份服务器上将 MASTER 改为 BACKUP</span><br><span class="line">    interface ens33 #网卡</span><br><span class="line">    virtual_router_id 51 #主、备机的 virtual_router_id 必须相同</span><br><span class="line">    priority 90 #主、备机取不同的优先级，主机值较大，备份机值较小</span><br><span class="line">    advert_int 1</span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1111</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.17.50 #VRRP H 虚拟地址</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>2）脚本文件 nginx_check.sh：</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">#!/bin/bash</span><br><span class="line">A=`ps -C nginx –no-header |wc -l`</span><br><span class="line">if [ $A -eq 0 ];then</span><br><span class="line">    /usr/local/nginx/sbin/nginx</span><br><span class="line">    sleep 2</span><br><span class="line">    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then</span><br><span class="line">        killall keepalived</span><br><span class="line">    fi</span><br><span class="line">fi</span><br></pre></td></tr></table></figure>
<h2 id="7-nginx原理"><a class="markdownIt-Anchor" href="#7-nginx原理">#</a> 7. Nginx 原理</h2>
<h3 id="71-master和worker"><a class="markdownIt-Anchor" href="#71-master和worker">#</a> 7.1 master 和 worker</h3>
<p><img data-src="Nginx/master&amp;worker0.jpg" alt=""><br>
<img data-src="Nginx/master&amp;worker.jpg" alt=""></p>
<h3 id="72-worker如何进行工作的"><a class="markdownIt-Anchor" href="#72-worker如何进行工作的">#</a> 7.2 worker 如何进行工作的</h3>
<p><img data-src="Nginx/worker%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86.jpg" alt=""></p>
<h3 id="73-一个master和多个worker有好处"><a class="markdownIt-Anchor" href="#73-一个master和多个worker有好处">#</a> 7.3 一个 master 和多个 worker 有好处</h3>
<ul>
<li>
<ol>
<li>可以使用  <code>nginx –s reload</code>  热部署，利用 nginx 进行热部署操作</li>
</ol>
</li>
<li>
<ol start="2">
<li>每个 worker 是独立的进程，如果有其中的一个 woker 出现问题，其他 woker 独立的，继续进行争抢，实现请求过程，不会造成服务中断</li>
</ol>
</li>
</ul>
<h3 id="74-设置多少个worker合适"><a class="markdownIt-Anchor" href="#74-设置多少个worker合适">#</a> 7.4 设置多少个 worker 合适</h3>
<ul>
<li>worker 数和服务器的 cpu 数相等是最为适宜的</li>
</ul>
<h3 id="75-连接数worker_connection"><a class="markdownIt-Anchor" href="#75-连接数worker_connection">#</a> 7.5 连接数 worker_connection</h3>
<ul>
<li>第一个：发送请求，占用了 woker 的几个连接数？ 2 或者 4 个</li>
<li>第二个：nginx 有一个 master，有四个 woker，每个 woker 支持最大的连接数 1024，支持的最大并发数是多少？
<ul>
<li>普通的静态访问最大并发数是：worker_connections * worker_processes / 2，</li>
<li>而如果是 HTTP 作为反向代理来说，最大并发数量应该是 worker_connections * worker_processes/4。</li>
</ul>
</li>
</ul>

      <div class="tags">
          <a href="/blog/tags/Nginx/" rel="tag"><i class="ic i-tag"></i> Nginx</a>
      </div>
  </div>

   <footer>

    <div class="meta">
  <span class="item">
    <span class="icon">
      <i class="ic i-calendar-check"></i>
    </span>
    <span class="text">更新于</span>
    <time title="修改时间：2021-11-27 18:36:07" itemprop="dateModified" datetime="2021-11-27T18:36:07+08:00">2021-11-27</time>
  </span>
  <span id="2020/03/03/Nginx/" class="item leancloud_visitors" data-flag-title="Nginx 学习笔记" title="阅读次数">
      <span class="icon">
        <i class="ic i-eye"></i>
      </span>
      <span class="text">阅读次数</span>
      <span class="leancloud-visitors-count"></span>
      <span class="text">次</span>
  </span>
</div>

      
<div class="reward">
  <button><i class="ic i-heartbeat"></i> 赞赏</button>
  <p>请我喝[茶]~(￣▽￣)~*</p>
  <div id="qr">
      
      <div>
        <img data-src="/blog/images/wechatpay.png" alt="Rain Bear 微信支付">
        <p>微信支付</p>
      </div>
      
      <div>
        <img data-src="/blog/images/alipay.png" alt="Rain Bear 支付宝">
        <p>支付宝</p>
      </div>
      
      <div>
        <img data-src="/blog/images/paypal.png" alt="Rain Bear 贝宝">
        <p>贝宝</p>
      </div>
  </div>
</div>

      

<div id="copyright">
<ul>
  <li class="author">
    <strong>Rain Bear： </strong>Rain Bear <i class="ic i-at"><em>@</em></i>Endless
  </li>
  <li class="link">
    <strong>本文链接：</strong>
    <a href="http://fairy77.gitee.io/2020/03/03/Nginx/" title="Nginx 学习笔记">http://fairy77.gitee.io/2020/03/03/Nginx/</a>
  </li>
  <li class="license">
    <strong>版权声明： </strong>本站所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

  </footer>

</article>

  </div>
  

<div class="post-nav">
    <div class="item left">
      

  <a href="/blog/2020/03/01/linux-basic-operation/" itemprop="url" rel="prev" data-background-image="https:&#x2F;&#x2F;tva1.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1giciundwu5j20zk0m8n9e.jpg" title="Linux基本操作">
  <span class="type">上一篇</span>
  <span class="category"><i class="ic i-flag"></i> </span>
  <h3>Linux基本操作</h3>
  </a>

    </div>
    <div class="item right">
      

  <a href="/blog/2020/03/08/SpringBoot/" itemprop="url" rel="next" data-background-image="https:&#x2F;&#x2F;tva1.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1gipey84bjtj20zk0m8hdt.jpg" title="SpringBoot学习笔记1">
  <span class="type">下一篇</span>
  <span class="category"><i class="ic i-flag"></i> </span>
  <h3>SpringBoot学习笔记1</h3>
  </a>

    </div>
</div>

  
  <div class="wrap" id="comments"></div>


        </div>
        <div id="sidebar">
          

<div class="inner">

  <div class="panels">
    <div class="inner">
      <div class="contents panel pjax" data-title="文章目录">
          <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-nginx%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="toc-number">1.</span> <span class="toc-text"> 1. Nginx 基本概念</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#11-%E4%BB%80%E4%B9%88%E6%98%AFnginx"><span class="toc-number">1.1.</span> <span class="toc-text"> 1.1 什么是 nginx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.</span> <span class="toc-text"> 1.2 反向代理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#121-%E6%AD%A3%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.1.</span> <span class="toc-text"> 1.2.1 正向代理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#122-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.2.2.</span> <span class="toc-text"> 1.2.2 反向代理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#13-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">1.3.</span> <span class="toc-text"> 1.3 负载均衡</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#14-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">1.4.</span> <span class="toc-text"> 1.4 动静分离</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-nginx%E5%AE%89%E8%A3%85"><span class="toc-number">2.</span> <span class="toc-text"> 2. Nginx 安装</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#21-%E5%9C%A8linux%E7%B3%BB%E7%BB%9F%E4%B8%AD%E5%AE%89%E8%A3%85nginx"><span class="toc-number">2.1.</span> <span class="toc-text"> 2.1 在 Linux 系统中安装 nginx</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#22-nginx%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">2.2.</span> <span class="toc-text"> 2.2 nginx 常用命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#23-nginx%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">2.3.</span> <span class="toc-text"> 2.3 nginx 配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#231-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE%E6%A0%B9%E6%8D%AE%E5%AE%89%E8%A3%85%E7%B1%BB%E5%9E%8B%E4%B8%8D%E5%90%8C%E4%BD%8D%E7%BD%AE%E5%8F%AF%E8%83%BD%E4%B8%8D%E4%B8%80%E6%A0%B7%E4%B9%9F%E5%8F%AF%E4%BB%A5%E8%87%AA%E8%A1%8C%E9%85%8D%E7%BD%AE%E5%9C%B0%E5%9D%80"><span class="toc-number">2.3.1.</span> <span class="toc-text"> 2.3.1 配置文件位置（根据安装类型不同，位置可能不一样，也可以自行配置地址）</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#232-nginx%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E7%BB%84%E6%88%90"><span class="toc-number">2.3.2.</span> <span class="toc-text"> 2.3.2 nginx 配置文件组成</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-nginx%E9%85%8D%E7%BD%AE%E5%AE%9E%E4%BE%8B1-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">3.</span> <span class="toc-text"> 3. Nginx 配置实例 1 - 反向代理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#31-%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C"><span class="toc-number">3.1.</span> <span class="toc-text"> 3.1 实现效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#32-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">3.2.</span> <span class="toc-text"> 3.2 准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#33-%E8%AE%BF%E9%97%AE%E8%BF%87%E7%A8%8B%E5%88%86%E6%9E%90"><span class="toc-number">3.3.</span> <span class="toc-text"> 3.3 访问过程分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#34-nginx%E9%85%8D%E7%BD%AE"><span class="toc-number">3.4.</span> <span class="toc-text"> 3.4 nginx 配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-nginx%E9%85%8D%E7%BD%AE%E5%AE%9E%E4%BE%8B2-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">4.</span> <span class="toc-text"> 4. Nginx 配置实例 2 - 负载均衡</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#41-%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C"><span class="toc-number">4.1.</span> <span class="toc-text"> 4.1 实现效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#42-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">4.2.</span> <span class="toc-text"> 4.2 准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#43-%E5%9C%A8nginx%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%B8%AD%E8%BF%9B%E8%A1%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-number">4.3.</span> <span class="toc-text"> 4.3 在 nginx 的配置文件中进行负载均衡的配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#44-nginx%E5%88%86%E9%85%8D%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AD%96%E7%95%A5"><span class="toc-number">4.4.</span> <span class="toc-text"> 4.4 nginx 分配服务器策略</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-nginx%E9%85%8D%E7%BD%AE%E5%AE%9E%E4%BE%8B3-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">5.</span> <span class="toc-text"> 5. Nginx 配置实例 3 - 动静分离</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#51-%E4%BB%80%E4%B9%88%E6%98%AF%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">5.1.</span> <span class="toc-text"> 5.1 什么是动静分离</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#52-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">5.2.</span> <span class="toc-text"> 5.2 准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#53-%E5%85%B7%E4%BD%93%E9%85%8D%E7%BD%AE"><span class="toc-number">5.3.</span> <span class="toc-text"> 5.3 具体配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-nginx%E9%85%8D%E7%BD%AE%E9%AB%98%E5%8F%AF%E7%94%A8%E9%9B%86%E7%BE%A4"><span class="toc-number">6.</span> <span class="toc-text"> 6. Nginx 配置高可用集群</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#61-%E4%BB%80%E4%B9%88%E6%98%AFnginx%E9%AB%98%E5%8F%AF%E7%94%A8"><span class="toc-number">6.1.</span> <span class="toc-text"> 6.1 什么是 nginx 高可用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#62-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">6.2.</span> <span class="toc-text"> 6.2 准备工作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#63-%E5%AE%89%E8%A3%85keepalived"><span class="toc-number">6.3.</span> <span class="toc-text"> 6.3 安装 keepalived</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#64-%E9%85%8D%E7%BD%AE"><span class="toc-number">6.4.</span> <span class="toc-text"> 6.4 配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-nginx%E5%8E%9F%E7%90%86"><span class="toc-number">7.</span> <span class="toc-text"> 7. Nginx 原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#71-master%E5%92%8Cworker"><span class="toc-number">7.1.</span> <span class="toc-text"> 7.1 master 和 worker</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#72-worker%E5%A6%82%E4%BD%95%E8%BF%9B%E8%A1%8C%E5%B7%A5%E4%BD%9C%E7%9A%84"><span class="toc-number">7.2.</span> <span class="toc-text"> 7.2 worker 如何进行工作的</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#73-%E4%B8%80%E4%B8%AAmaster%E5%92%8C%E5%A4%9A%E4%B8%AAworker%E6%9C%89%E5%A5%BD%E5%A4%84"><span class="toc-number">7.3.</span> <span class="toc-text"> 7.3 一个 master 和多个 worker 有好处</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#74-%E8%AE%BE%E7%BD%AE%E5%A4%9A%E5%B0%91%E4%B8%AAworker%E5%90%88%E9%80%82"><span class="toc-number">7.4.</span> <span class="toc-text"> 7.4 设置多少个 worker 合适</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#75-%E8%BF%9E%E6%8E%A5%E6%95%B0worker_connection"><span class="toc-number">7.5.</span> <span class="toc-text"> 7.5 连接数 worker_connection</span></a></li></ol></li></ol>
      </div>
      <div class="related panel pjax" data-title="系列文章">
      </div>
      <div class="overview panel" data-title="站点概览">
        <div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="image" itemprop="image" alt="Rain Bear"
      data-src="/blog/images/avatar.jpg">
  <p class="name" itemprop="name">Rain Bear</p>
  <div class="description" itemprop="description"></div>
</div>

<nav class="state">
    <div class="item posts">
      <a href="/blog/archives/">
        <span class="count">11</span>
        <span class="name">文章</span>
      </a>
    </div>
    <div class="item tags">
      <a href="/blog/tags/">
        <span class="count">8</span>
        <span class="name">标签</span>
      </a>
    </div>
</nav>

<div class="social">
      <span class="exturl item github" data-url="aHR0cHM6Ly9naXRodWIuY29tL2ZhaXJ5Nw==" title="https:&#x2F;&#x2F;github.com&#x2F;fairy7"><i class="ic i-github"></i></span>
      <span class="exturl item zhihu" data-url="aHR0cHM6Ly9naXRlZS5jb20vZmFpcnk3Nw==" title="https:&#x2F;&#x2F;gitee.com&#x2F;fairy77"><i class="ic i-gitee"></i></span>
      <span class="exturl item music" data-url="aHR0cHM6Ly9tdXNpYy4xNjMuY29tLyMvdXNlci9ob21lP2lkPTI1Njk2NTkxNg==" title="https:&#x2F;&#x2F;music.163.com&#x2F;#&#x2F;user&#x2F;home?id&#x3D;256965916"><i class="ic i-cloud-music"></i></span>
</div>

<ul class="menu">
  
    
  <li class="item">
    <a href="/blog/" rel="section"><i class="ic i-home"></i>首页</a>
  </li>

    
  <li class="item">
    <a href="/blog/about/" rel="section"><i class="ic i-user"></i>关于</a>
  </li>

        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a>
  </li>

        
  <li class="item">
    <a href="/blog/categories/" rel="section"><i class="ic i-th"></i>分类</a>
  </li>

        
  <li class="item">
    <a href="/blog/tags/" rel="section"><i class="ic i-tags"></i>标签</a>
  </li>

  </ul>
    
  <li class="item">
    <a href="/blog/friends/" rel="section"><i class="ic i-heart"></i>friends</a>
  </li>

    
  <li class="item">
    <a href="/blog/links/" rel="section"><i class="ic i-magic"></i>links</a>
  </li>


</ul>

      </div>
    </div>
  </div>

  <ul id="quick">
    <li class="prev pjax">
        <a href="/blog/2020/03/01/linux-basic-operation/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a>
    </li>
    <li class="up"><i class="ic i-arrow-up"></i></li>
    <li class="down"><i class="ic i-arrow-down"></i></li>
    <li class="next pjax">
        <a href="/blog/2020/03/08/SpringBoot/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a>
    </li>
    <li class="percent"></li>
  </ul>
</div>


        </div>
        <div class="dimmer"></div>
      </div>
    </main>
    <footer id="footer">
      <div class="inner">
        <div class="widgets">
          
<div class="rpost pjax">
  <h2>随机文章</h2>
  <ul>
      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/02/28/%E8%8E%B7%E5%8F%96%E7%94%A8%E9%94%AE%E7%9B%98%E8%BE%93%E5%85%A5%E5%B8%B8%E7%94%A8%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%96%B9%E6%B3%95/" title="获取用键盘输入常用的两种方法">获取用键盘输入常用的两种方法</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/08/SpringBoot/" title="SpringBoot学习笔记1">SpringBoot学习笔记1</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/02/29/git-operation/" title="Git&amp;Github">Git&Github</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/01/linux-basic-operation/" title="Linux基本操作">Linux基本操作</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/02/29/MySQL%E5%AE%89%E8%A3%85/" title="MySQL安装">MySQL安装</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/19/%E6%95%B0%E7%BB%84%E7%9A%84%E4%BD%BF%E7%94%A8/" title="数组的使用">数组的使用</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2021/11/19/tomcat/" title="tomcat">tomcat</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/03/Nginx/" title="Nginx学习笔记">Nginx学习笔记</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/01/%E9%85%8D%E7%BD%AELinux%E8%99%9A%E6%8B%9F%E6%9C%BA/" title="配置Linux虚拟机">配置Linux虚拟机</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
</div>

    <span><a href="/blog/2020/03/20/MySQL%E7%B4%A2%E5%BC%95%E5%8E%9F%E7%90%86/" title="MySQL索引原理">MySQL索引原理</a></span>
  </li>

  </ul>
</div>
<div>
  <h2>最新评论</h2>
  <ul class="leancloud-recent-comment"></ul>
</div>

        </div>
        <div class="status">
  <div class="copyright">
    
    &copy; 2019 – 
    <span itemprop="copyrightYear">2021</span>
    <span class="with-love">
      <i class="ic i-sakura rotate"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Rain Bear @ 夏季大三角</span>
  </div>
  <div class="count">
    <span class="post-meta-item-icon">
      <i class="ic i-chart-area"></i>
    </span>
    <span title="站点总字数">106k 字</span>

    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="ic i-coffee"></i>
    </span>
    <span title="站点阅读时长">1:36</span>
  </div>
  <div class="powered-by">
    基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span>
  </div>
</div>

      </div>
    </footer>
  </div>
<script data-config type="text/javascript">
  var LOCAL = {
    path: '2020/03/03/Nginx/',
    favicon: {
      show: "（●´3｀●）やれやれだぜ",
      hide: "(´Д｀)大変だ！"
    },
    search : {
      placeholder: "文章搜索",
      empty: "关于 「 ${query} 」，什么也没搜到",
      stats: "${time} ms 内找到 ${hits} 条结果"
    },
    valine: true,fancybox: true,
    copyright: '复制成功，转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',
    ignores : [
      function(uri) {
        return uri.includes('#');
      },
      function(uri) {
        return new RegExp(LOCAL.path+"$").test(uri);
      }
    ]
  };
</script>

<script src="https://cdn.polyfill.io/v2/polyfill.js"></script>

<script src="//cdn.jsdelivr.net/combine/npm/pace-js@1.0.2/pace.min.js,npm/pjax@0.2.8/pjax.min.js,npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js,npm/animejs@3.2.0/lib/anime.min.js,npm/algoliasearch@4/dist/algoliasearch-lite.umd.js,npm/instantsearch.js@4/dist/instantsearch.production.min.js,npm/lozad@1/dist/lozad.min.js,npm/quicklink@2/dist/quicklink.umd.js"></script>

<script src="/blog/js/app.js?v=0.2.5"></script>




</body>
</html>
